/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2012-2021. All rights reserved.
 */

package com.huawei.devsecurity.secapp.demo;

import com.huaweicloud.sdk.core.auth.BasicCredentials;
import com.huaweicloud.sdk.core.auth.ICredential;
import com.huaweicloud.sdk.core.exception.ClientRequestException;
import com.huaweicloud.sdk.core.exception.ServerResponseException;
import com.huaweicloud.sdk.core.http.FormDataFilePart;
import com.huaweicloud.sdk.devsecurity.v1.DevSecurityClient;
import com.huaweicloud.sdk.devsecurity.v1.model.CreateSecAppTaskRequest;
import com.huaweicloud.sdk.devsecurity.v1.model.CreateSecAppTaskRequestBody;
import com.huaweicloud.sdk.devsecurity.v1.model.CreateSecAppTaskResponse;
import com.huaweicloud.sdk.devsecurity.v1.region.DevSecurityRegion;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

public class CreateSecAppTaskDemo {
    private static final Logger logger = LoggerFactory.getLogger(CreateSecAppTaskDemo.class.getName());

    public static void main(String[] args) throws IOException {
        String ak = "<YOUR AK>";
        String sk = "<YOUR SK>";

        // 创建DevSecurityClient实例
        ICredential auth = new BasicCredentials().withAk(ak).withSk(sk);
        DevSecurityClient client = DevSecurityClient.newBuilder().withCredential(auth).withRegion(DevSecurityRegion.CN_NORTH_4).build();

        // 创建<创建移动应用安全任务并启动>请求，并指定待扫描的文件和版本
        CreateSecAppTaskRequestBody createSecAppTaskRequestBody = new CreateSecAppTaskRequestBody();
        InputStream inputstream = new FileInputStream("<YOUR FILE_PATH>"); // 文件绝对路径
        FormDataFilePart file = new FormDataFilePart(inputstream, "<YOUR FILE_NAME>"); // 文件名，仅支持apk/hap文件
        createSecAppTaskRequestBody.withFile(file);
        createSecAppTaskRequestBody.withVersion(0); // 0免费版 1专业版
        CreateSecAppTaskRequest createSecAppTaskRequest = new CreateSecAppTaskRequest().withBody(createSecAppTaskRequestBody);
        try {
            // 执行创建移动应用安全任务并启动请求
            CreateSecAppTaskResponse createSecAppTaskResponse = client.createSecAppTask(createSecAppTaskRequest);
            logger.info(createSecAppTaskResponse.toString());
        } catch (ClientRequestException | ServerResponseException e) {
            logger.error(String.valueOf(e.getHttpStatusCode()));
            logger.error(e.toString());
        }
    }
}
